<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/24
 * Time: 13:58
 */

namespace app\common\model\mall;


use app\common\model\common\Categories;
use app\common\model\TimeModel;

class Account extends TimeModel
{
    protected $deleteTime = 'delete_time';

    public function getImageAttr($value){
        return explode(',',$value);
    }

    //课程所属分类
    public function category()
    {
        return $this->belongsTo('app\common\model\common\Categories','category_id','id')->field('id,name');
    }

    //课程所属商家
    public function store()
    {
        return $this->belongsTo('app\common\model\mall\Store','store_id','id')->field('id,name');
    }
    //下所有的规格
    public function items()
    {
        return $this->hasMany('app\common\model\media\AccountItem','article_id','id');
    }
    //与课程收藏对多关联
    public function collect()
    {
        return $this->belongsToMany('App\Models\Collect','collect','account_id','user_id');
    }
    //与课程标签多对多关联
    public function tags()
    {
        return $this->belongsToMany('app\common\model\common\Tags','relation_tag','other_id','tag_id');
    }

    //查看评价
    public function comments()
    {
        return $this->belongsTo('App\Models\OrderComment','id','goods_id');
    }

   /** 遍历数据，查找parId为参数$parid指定的id*/

    public function getAllNextId($id,$data=[]){
        $pids = Categories::where('parent_id',$id)->column('id')
           ;
        if(count($pids)>0){
            foreach($pids as $v){
                $data[] = $v;
                $data = $this->getAllNextId($v,$data); //注意写$data 返回给上级
            }
        }
        if(count($data)>0){
            return $data;
        }else{
            return [];
        }
    }
}